package com.itboye.lottery.fragment.home.football


import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.handmark.pulltorefresh.library.ObservableScrollView
import com.handmark.pulltorefresh.library.PullToRefreshBase
import com.handmark.pulltorefresh.library.xrecyclerview.ProgressStyle
import com.handmark.pulltorefresh.library.xrecyclerview.XRecyclerView
import com.handmark.pulltorefresh.library.xrecyclerview.XRecyclerViewStopLoadAndRefreshUtil.stopLoadAndRefresh
import com.itboye.lottery.base.BaseFragment
import com.itboye.lottery.R
import com.itboye.lottery.activity.daletou.BascketballMainActivity
import com.itboye.lottery.activity.daletou.DaLeTouMainActivity
import com.itboye.lottery.activity.follow.FollowDetailActivity
import com.itboye.lottery.activity.football.FootballMainActivity
import com.itboye.lottery.activity.shuangseqiu.ShuangSeQiuMainActivity
import com.itboye.lottery.activity.user.AddressListActivity
import com.itboye.lottery.activity.user.TouzhuDetailActivity
import com.itboye.lottery.adapter.TouZhuAdapter
import com.itboye.lottery.alert.ByAlert
import com.itboye.lottery.bean.BetBean
import com.itboye.lottery.bean.OrderBean
import com.itboye.lottery.callback.PayCallBack
import com.itboye.lottery.dialog.PayDialog
import com.itboye.lottery.presenter.UserPresenter
import com.itboye.lottery.retrofit.ResponseCallback
import com.itboye.lottery.utils.Const
import kotlinx.android.synthetic.main.fragment_tou_zhu.*


/**
 * A simple [Fragment] subclass.
 */
class TouZhuFragment : BaseFragment(), TouZhuAdapter.OnItemClickListener, ResponseCallback {
    override fun initData() {
        getTouZhuData()
        initPullToRefresh()
    }

    override fun success(o: Any, requestSuccessType: String) {
        pull_to_refresh_scrollView.onRefreshComplete()
        if (page == 1) {
            betList.clear()
        }
        betList.addAll(o as List<BetBean>)
        adapter.notifyDataSetChanged()

    }

    override fun fail(code: Int, msg: Any, requestErrorType: String) {
        ByAlert.alert(msg)
        pull_to_refresh_scrollView.onRefreshComplete()
    }


    var userPresenter: UserPresenter = UserPresenter(this)
    var type: Int = 0
    var type2: Int = 0
    override fun onItemClick(view: View?, position: Int) {
        when (view!!.id) {
            R.id.item_touzhu_root -> {
                if (type2 == 0 ) {
                    startActivity(Intent(activity, TouzhuDetailActivity::class.java)
                            .putExtra("id", betList[position].id)
                            .putExtra("gamecate", betList[position].gamecate)
                            .putExtra("cid", if (betList[position].cid == null) {
                                0
                            } else {
                                betList[position].cid.toInt()}
                            ))
                } else if (type2 == 1) {
                    startActivity(Intent(activity, FollowDetailActivity::class.java)
                            .putExtra("type", betList[position].lottery_type)
                            .putExtra("orderType", 1)
                            .putExtra("lu_id", betList[position].lu_id)
                            .putExtra("order_id", betList[position].id)
                    )
                } else if (type2 == 2) {
                    startActivity(Intent(activity, FollowDetailActivity::class.java)
                            .putExtra("type", betList[position].lottery_type)
                            .putExtra("orderType", 2)
                            .putExtra("order_id", betList[position].id)
                    )
                }
            }
            R.id.iv_item_touzhu_youji_caipiao -> {
                startActivity(Intent(activity, AddressListActivity::class.java))
            }
            else -> {
            }
        }
    }

    companion object {
        fun newInstance(type: Int, type2: Int): TouZhuFragment {
            val fragment = TouZhuFragment()
            val bundle = Bundle()
            bundle.putInt("type", type)
            bundle.putInt("type2", type2)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_tou_zhu
    }


    var page = 1
    fun getTouZhuData() {
        type = arguments!!.getInt("type")
        type2 = arguments!!.getInt("type2")
        when (type2) {
            0 -> {
                userPresenter!!.getBetList(type, page)//获取投注记录
            }
            1 -> {
                //我的跟单
                userPresenter!!.myGenDan(type, page, 10)
            }
            2 -> {
                //我的推荐
                userPresenter!!.myTuiJian(type, page, 10)
            }
            else -> {
            }
        }
    }

    private fun initPullToRefresh() {
        rv_touzhu_record.layoutManager = LinearLayoutManager(activity)
//        rv_touzhu_record.addItemDecoration(DividerItemDecoration(activity, DividerItemDecoration.VERTICAL))
        adapter = TouZhuAdapter(this, betList)
        adapter.setType2(type2)
        adapter.setOnItemClickListener(this)
        rv_touzhu_record.adapter = adapter
        pull_to_refresh_scrollView.mode = PullToRefreshBase.Mode.BOTH
        pull_to_refresh_scrollView.setOnRefreshListener(object : PullToRefreshBase.OnRefreshListener2<ObservableScrollView> {
            override fun onPullDownToRefresh(refreshView: PullToRefreshBase<ObservableScrollView>) {
                page = 1
                getTouZhuData()
            }

            override fun onPullUpToRefresh(refreshView: PullToRefreshBase<ObservableScrollView>) {
                page++
                getTouZhuData()
            }
        })
    }

    private lateinit var adapter: TouZhuAdapter
    var betList: ArrayList<BetBean> = ArrayList()

    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.iv_item_touzhu_youji_caipiao -> {
//                startActivity(Intent(activity, AddressListActivity::class.java))
            }
            R.id.iv_item_touzhu_goon -> {//收入详情
                //继续投注
                var gamecate = p0!!.getTag(R.id.tag_1).toString()
                //2001 大乐透 1001 双色球 3010 足球  3001篮球
                when (gamecate) {
                    "2001" -> {
                        if (!Const.daletou) {
                            ByAlert.alert("投注已关闭")
                            return
                        }
                        startActivity(Intent(activity, DaLeTouMainActivity::class.java))
                    }
                    "1001" -> {
                        if (!Const.shuangseqiu) {
                            ByAlert.alert("投注已关闭")
                            return
                        }
                        startActivity(Intent(activity, ShuangSeQiuMainActivity::class.java))
                    }
                    "3010", "0" -> {
                        if (!Const.zq) {
                            ByAlert.alert("投注已关闭")
                            return
                        }
                        startActivity(Intent(activity, FootballMainActivity::class.java))
                    }
                    else -> {
                        if (!Const.lq) {
                            ByAlert.alert("投注已关闭")
                            return
                        }
                        startActivity(Intent(activity, BascketballMainActivity::class.java))
                    }
                }
            }
            R.id.iv_item_touzhu_pay -> {//立即跟单、支付、收入详情
                var position = p0!!.tag as Int
//                when (type2) {
//                    0 -> {//投注记录 点击 对应 立即支付
                var payDialog = PayDialog(activity, true)
                var money = when (type2) {
                    0 -> {
                        betList[position].money
                    }
                    else -> {
                        betList[position].total_amount
                    }
                }
                var orderBean = OrderBean(betList[position].sn, money.toDouble())
                payDialog.setOrderBean(orderBean)
                payDialog.setCallBack(object : PayCallBack {
                    override fun paySuccess() {
//                                getTouZhuData()
                        betList[position].pay_status = 1
                        adapter.notifyDataSetChanged()
                    }

                    override fun payFail(msg: String) {
                        ByAlert.alert(msg)
                    }

                })
                payDialog.show()
//                    }
//                    else -> {
//                    }
//                }
            }
            else -> {
            }
        }
    }

}
